package com.graduate.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduate.common.entity.House;
import com.graduate.common.entity.Reserve;
import com.graduate.common.exception.UserException;
import com.graduate.common.mapper.HouseMapper;
import com.graduate.common.mapper.ReserveMapper;
import com.graduate.common.service.IReserveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduate.common.utils.PageUtils;
import com.graduate.common.vo.HouseVo;
import com.graduate.common.vo.ReserveVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zfy
 * @since 2021-05-09
 */
@SuppressWarnings("ALL")
@Service
public class ReserveServiceImpl extends ServiceImpl<ReserveMapper, Reserve> implements IReserveService {

    // 查询房间的mapper
    @Autowired
    private HouseMapper houseMapper;

    @Override
    public void reserveHouse(Reserve reserve) throws UserException {
        // 先判断一下当前的这个预定信息中的身份证号码有没有已经被预定了
//        QueryWrapper<Reserve> wrapper = new QueryWrapper<>();
//        wrapper.eq("identificationNum", reserve.getIdentificationNum());
//        Reserve one = baseMapper.selectOne(wrapper);
//        if (one != null) {
//            // 如果能查询到就说明当前的用户已经约定过了
//            throw new UserException("当前身份证号码已经预定过了");
//        }
        // 直接把封装好的预定信息，插入数据库中
        int insert = baseMapper.insert(reserve);
        if (insert == 0) {
            throw new UserException("预定失败");
        }
    }

    @Override
    public ReserveVo selectReserveByPhoneOrName(String telephone, String realName) {
        QueryWrapper<Reserve> wrapper = new QueryWrapper<>();
        if (telephone != null && !"".equals(telephone)) {
            // 如果存在电话号码就把电话号码的条件放入，电话号码是精确匹配
            wrapper.eq("telephone", telephone);
        }
        if (realName != null && !"".equals(realName)) {
            wrapper.like("realName", "%" + realName + "%");
        }
        Reserve reserve = baseMapper.selectOne(wrapper);
        // 查询出hid之后查询对应的house对象
        House house = houseMapper.selectById(reserve.getHid());
        ReserveVo reserveVo = new ReserveVo();
        BeanUtils.copyProperties(reserve, reserveVo);
        reserveVo.setHouse(house);
        return reserveVo;
    }

    @Override
    public IPage<ReserveVo> selectReservePage(Map<String, Object> map) {
        // 设置分页查询对象
        IPage<Reserve> page = (IPage<Reserve>) PageUtils.convertToPage(map);
        QueryWrapper<Reserve> wrapper = new QueryWrapper<>();
        if (map.get("telephone") != null && !"".equals(map.get("telephone"))) {
            wrapper.eq("telephone", map.get("telephone"));
        }
        if (map.get("realName") != null && !"".equals("realName")) {
            wrapper.eq("realName", map.get("realName"));
        }
        // page为查询出的分页对象
        page = baseMapper.selectPage(page, wrapper);
        // 取出page中的数据转换成VO对象
        List<Reserve> records = page.getRecords();
        List<ReserveVo> houseVos = records.stream().map(record -> {
            ReserveVo reserveVo = new ReserveVo();
            // 把数据库中的预定信息全部封装进入vo对象中
            BeanUtils.copyProperties(record, reserveVo);
            // 从数据库中查询出house信息封装进入vo对象中
            House house = houseMapper.selectById(record.getHid());
            reserveVo.setHouse(house);
            return reserveVo;
        }).collect(Collectors.toList());
        Page<ReserveVo> ans = new Page<>();
        BeanUtils.copyProperties(page, ans);
        ans.setRecords(houseVos);
        return ans;
    }

}
